<template>
  <div class="merak-stat-card">
  
    <template v-for="group in props.data">
      <div :class="['merak-stat-box', { 'merak-stat-box--active': group.some(item => model === item.key) }]"> 
        <template v-for="item in group">
         
          <div
            :class="[
              'merak-stat-item',
              { 'merak-stat-item--active': model === item.key },
              { 'merak-stat-item--horizontal': !!item.horizontal }
            ]"
            @click="handleClick(item)"
          >
            <span>{{ item.label }}</span>
            <div class="merak-stat-item--text">
              <span>{{ item.value }}</span>
              <span v-if="!!item.unit">{{ item.unit }}</span>
            </div>
          </div>
        </template>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { MerakStatDataType } from "./index";

const props = defineProps<{ data: MerakStatDataType[][] }>();

const model = defineModel<string>();
console.log(model,"model值。。。。")
const emit = defineEmits<{
  change: [params: Record<string, string | number>];
}>();

const handleClick = (item: MerakStatDataType) => {
  const flag = model.value === item.key;
  model.value = flag ? "" : item.key;
  emit("change", flag ? {} : { [item.search_key]: item.search_val });
};
</script>

<style lang="scss">
.dark .merak-stat-card {
  --merak-stat-hover-color: #1d1d1d;
}
.merak-stat-card {
  --merak-stat-hover-color: #f5f8fa;
  display: flex;
  & > :not(:first-child) {
    margin-left: var(--merak-space);
  }
  .merak-stat-box {
    display: flex;
    flex: auto;
    align-items: center;
    border: 1px solid transparent;
    border-radius: 6px;
    box-shadow: var(--merak-shadow);
    box-sizing: border-box;
    overflow: hidden;
    transition: border-color 0.5s;
    &--active {
      border-color: var(--el-color-primary);
    }
  }
  .merak-stat-item {
    position: relative;
    display: flex;
    flex: auto;
    flex-direction: column;
    flex-basis: 120px;
    align-items: center;
    justify-content: center;
    padding: 20px;
    height: 100%;
    color: var(--merak-text-color);
    font-size: 14px;
    box-sizing: border-box;
    transition:
      color 0.5s,
      background-color 0.5s;
    cursor: pointer;
    & > :not(:first-child) {
      margin-top: 9px;
    }
    &:not(:first-child)::before {
      content: "";
      position: absolute;
      top: 50%;
      left: 0;
      width: 1px;
      height: 40%;
      background-color: var(--merak-border-color);
      transform: translateY(-50%);
    }
    &:hover {
      background-color: var(--merak-stat-hover-color);
    }
    &--horizontal {
      flex-direction: row;
      flex-basis: 220px;
      font-size: 20px;
      & > :last-child {
        margin-top: 0;
        margin-left: var(--merak-padding);
        & > span:nth-child(1) {
          font-size: 28px;
        }
      }
    }
    &--active {
      color: var(--el-color-primary);
    }
    &--text {
      font-size: var(--merak-text-size);
      & > :not(:first-child) {
        margin-left: var(--merak-button-icon-padding);
      }
      & > span:nth-child(1) {
        font-size: 16px;
        font-weight: 500;
      }
    }
  }
}
</style>
